package org.example.seckill.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author woopsion
 * @date 2025-07-05
 */

@Configuration
public class RabbitMqConfig {
    //创建队列名称
    private static final String Queue="queue";
    public static final String QUEUE_1 = "fanout_queue1";
    public static final String QUEUE_2 = "fanout_queue2";
    public static final String EXCHANGE_FANOUT = "exchange-fanout";
    public static final String EXCHANGE_DIRECT = "exchnage-direct";
    /**
     * 配置队列
     * 队列QUEUE(queue)
     * true代表持久胡
     * durable队列是否持久化
     * 队列默认情况下放入在内存中，rabbitMq重启后就失效了
     * 如果希望队列重启后能够依旧读取数据 就需要持久化
     * Erlang 自带Mnesia数据库，rabbitmq重启后
     * 会读取该数据库
     */
    @Bean
    public Queue queue(){
        return  new Queue(Queue,true);
    }
    @Bean
    public Queue queue1() {
        //创建一个队列队列，并指定队列的名字和持久化
        return new Queue(QUEUE_1,true);
    }
    @Bean
    public Queue queue2() {
        //创建一个队列队列，并指定队列的名字
        return new Queue(QUEUE_2,true);
    }
    /**
     * 创建交换机
     */
    @Bean
    public FanoutExchange exchange_fanout(){
        return new FanoutExchange(EXCHANGE_FANOUT);
    }
    @Bean
    public Exchange exchange_direct(){
        //定义一个direct类型的交换机，并指定持久化
        return ExchangeBuilder.directExchange(EXCHANGE_DIRECT).durable(true).build();
    }

    /**
     * 将队列绑定到指定的交换机上
     * @return
     */
    @Bean
    public Binding bindingQueue1() {
        //fanout模式不指定routingkey
        return BindingBuilder
                .bind(queue1())
                .to(exchange_fanout());
    }   
    @Bean
    public Binding bindingQueue2() {
        //fanout模式不指定routingkey
        return BindingBuilder
                .bind(queue2())
                .to(exchange_fanout());
    }

    @Bean     
    public Binding bindingQueue3() {         
        return BindingBuilder                 
                .bind(queue1()).
                to(exchange_direct())
                .with("pay").noargs();
    }
    
    @Bean     
    public Binding bindingQueue4() {
        return BindingBuilder                 
                .bind(queue2()).to(exchange_direct())
                .with("order").noargs();     
    } 
    
}